package com.personalblog.service.serviceImpl;

import com.personalblog.handler.ApiNotFindException;
import com.personalblog.mapper.TypeMapper;
import com.personalblog.po.Type;
import com.personalblog.service.TypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jxau_scf
 * @date 2021/3/23 - 21:18
 */
@Service
public class TypeServiceImpl implements TypeService {
    private static final Logger logger = LoggerFactory.getLogger(TypeServiceImpl.class);
    @Autowired
    private TypeMapper typeMapper;

    @Override
    public int insertType(Type type) {
        return typeMapper.insertType(type);
    }

    @Override
    public int deleteType(int typeId) {
        return typeMapper.deleteType(typeId);
    }

    @Override
    public int updateType(int typeId,Type type) {
        Type t = typeMapper.findTypeById(typeId);
        if (t == null){
            throw new ApiNotFindException("该类型不存在");
        }
        BeanUtils.copyProperties(type,t);
        return typeMapper.updateType(t);
    }

    @Override
    public Type findTypeById(int typeId) {
        return typeMapper.findTypeById(typeId);
    }

    @Override
    public Type findTypeByName(String typeName) {
        return typeMapper.findTypeByName(typeName);
    }

    @Override
    public List<Type> findTypes() {
        return typeMapper.findTypes();
    }

    @Override
    public List<Type> findTypesFetchBlogs() {
        return typeMapper.findTypesFetchBlogs();
    }
}
